fun Next_Number(n) =
  if n mod 2 = 0 then n div 2 
  else 3*n+1;
  
fun Seq_Length(m) =
  let  
    fun f(m, l) = 
      if m = 1 then l+1
      else f(Next_Number(m), l+1);
  in 
    f(m, 0)
  end;
  
fun Output(m, res, max) = 
  if m = 1000000 then res
  else 
    if max < Seq_Length(m) then Output(m+1, m, Seq_Length(m))
	else Output(m+1, res, max);
  
Output(1, 0, ~1);